<?php

namespace addons\csmitsm\library\xcore\xcore\base;

use addons\csmitsm\library\xcore\xcore\utils\XcCrontaskUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcDaoUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcIncludeFileUtils;
use think\Controller;

abstract class XcAToolsAddonsController extends Controller
{
    public function index()
    {
        $ini = XcIncludeFileUtils::parse_ini_addons_file("info.ini");
        $this->success($ini['title'] . $ini['name'] . $ini['version'], null, [], 999);
    }

    // http://fastadmin_addons_csmitsm.fa.net/addons/csmitsm/xc_tools/cron
    public function cron()
    {
        // if (!$this->request->isCli()) {
        //     $this->error('Only work at client!');
        // }
        error_reporting(0);
        set_time_limit(0);

        $crontasks = XcDaoUtils::getListBySql(new \app\admin\model\csmitsm\Xcflowcrontask(), []);
        $executeCrontasks = [];
        foreach ($crontasks as $crontask) {
            if (XcCrontaskUtils::canExecute($crontask->cronexpression)) {
                $executeCrontasks[] = $crontask;
            }
        }
        foreach ($executeCrontasks as $crontask) {
            [$result, $loggerMessage] = XcCrontaskUtils::execute($crontask->taskcls, $crontask->taskmethod);
            XcDaoUtils::create(new \app\admin\model\csmitsm\XcflowcrontaskRunlog(), [
                "xcflowcrontask_id" => $crontask->id,
                "issuccess" => ($result) ? "Y" : "N",
                "loggerMessage" => $loggerMessage,
                "createtime" => time(),
            ]);

            $nextimes = XcCrontaskUtils::nexttimes($crontask->cronexpression, 3);
            $crontask->save([
                "lastrun_issuccess" => ($result) ? "Y" : "N",
                "lastresult" => $loggerMessage,
                "lastruntime" => time(),
                "nextruntime1" => $nextimes[0],
                "nextruntime2" => $nextimes[1],
                "nextruntime3" => $nextimes[2],
            ]);
        }
    }

    public function guideThreadJob()
    {
        echo static::guideBlock(
            "异步开始模式",
            "",
            [
                static::guideBlockItem("配置修改:application/extra/queue.php", "
                return [
                    'connector' => 'database',
                    'expire'  => 60,
                    'default' => 'default',
                    'table'   => 'jobs',
                    'dsn'     => [
                        'type' => 'mysql',
                        'database' => 'local_csmitsm',
                        'hostname' => '127.0.0.1',
                        'username' => 'root',
                        'password' => 'root',
                        'prefix' => 'fa_',
                    ]    
                ];", "code"),
                static::guideBlockItem("数据库创建表", "
                CREATE TABLE `fa_jobs` (
                `id` INT(11) NOT NULL AUTO_INCREMENT,
                `queue` VARCHAR(255) NOT NULL,
                `payload` LONGTEXT NOT NULL,
                `attempts` TINYINT(3) UNSIGNED NOT NULL,
                `reserved` TINYINT(3) UNSIGNED NOT NULL,
                `reserved_at` INT(10) UNSIGNED DEFAULT NULL,
                `available_at` INT(10) UNSIGNED NOT NULL,
                `created_at` INT(10) UNSIGNED NOT NULL,
                PRIMARY KEY (`id`)
                ) ENGINE=INNODB DEFAULT CHARSET=utf8; "),
                static::guideBlockItem("启动服务, Dos或Shell执行命令:", "
                php think queue:work --daemon"),
            ]
        );
    }

    private static function guideBlock($title, $intro, $blockItems)
    {
        $html = "<center><h3>{$title}</h3></center>";
        foreach ($blockItems as $item) {
            $html .= $item;
        }
        return $html;
    }
    private static function guideBlockItem($title, $content, $type = 'normal')
    {
        $html = "<b>{$title}</b><BR>";
        $html .= "<pre style='border:1px solid #9c9c9c;padding:5px;margin:10px;'>{$content}</pre>";
        return $html;
    }
}
